我正在尝试获取一些链接并将它们存储在MySQL表中。它没有存储数据,但我收到一条错误消息(那个说“idk man,它没有进入那里”)。我只是不确定我做错了什么。这是我到目前为止所做的:
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "<font color='#00FF00'>CONNECTED TO DATABASE SUCCESSFULLY</font><br>";
}
//find open link record and insert
foreach ($result as $wtf)
{
//now show me
mysqli_query ($conn,"INSERT INTO carads (adlink) VALUES ('$wtf')");
echo "<font color='#FFFFFF'>INSERTING LINK INTO DATABASE: $wtf</font><br>";
}
$sql="SELECT 'adlink' FROM 'carads'";
$sqlresult = mysqli_query($conn,$sql );
//if its messed up tell me now
if
( !$sqlresult)
{
echo mysqli_error($conn);
die("<font size='20' color='#FF0000'>idk man, there was an error and the dataset didnt get in there</font><br>") ;
}
if ($sqlresult = mysqli_query($conn,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($sqlresult);
print_r ("<font size='18' color='#00FF00'>Car ads Database has %d ADLINKS from current city.\n</font><br>",$rowcount);
// Free result set
mysqli_free_result($result);
}
//close the connection
mysqli_close($conn);
我再次转过身来,需要聪明人的帮助。为什么不将链接存储在数据库中?
答案 0 :(得分:3)
单引号('
)用于表示字符文字。在您的select语句中,您尝试引用对象(列和表)名称,因此您不应该使用它们:
$sql="SELECT adlink FROM carads"; // Note the lack of quotes
$sqlresult = mysqli_query($conn, $sql);
答案 1 :(得分:1)
我修改了你的一些代码。您没有选择任何数据库。 sql string中也存在语法错误。试试这个
$conn = mysqli_connect($servername, $username, $password, 'db_name');
if (mysqli_connect_error()){
die(mysqli_connect_error());
}else{
echo "<font color='#00FF00'>CONNECTED TO DATABASE SUCCESSFULLY</font><br>";
}
foreach ($result as $wtf)
{
$result = mysqli_query ($conn,"INSERT INTO carads (adlink) VALUES ('$wtf')");
if(!$result){
die(mysqli_error($conn));
}
echo "<font color='#FFFFFF'>INSERTING LINK INTO DATABASE: $wtf</font><br>";
}
$sql = "SELECT adlink FROM carads"; //Removed single quotes from here
$sqlresult = mysqli_query($conn,$sql );
if(!$sqlresult){
echo mysqli_error($conn);
die("<font size='20' color='#FF0000'>idk man, there was an error and the dataset didnt get in there</font><br>") ;
}
$rowcount=mysqli_num_rows($sqlresult);
print_r ("<font size='18' color='#00FF00'>Car ads Database has %d ADLINKS from current city.\n</font><br>",$rowcount);
mysqli_free_result($result);
mysqli_close($conn);
答案 2 :(得分:0)
删除列和表的单引号或使用严重重音(`)。 我建议每当你不确定SQL查询时,尝试在phpmyadmin中执行相同的操作,并阅读它用来执行此操作的查询。