1

I'm trying to take some links and store them in a MySQL table. Its not storing the data but I am getting an error message(the one that says "idk man, it didnt get in there"). I'm just not sure what I'm doing wrong. Here's what I have so far:

     // 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);        

Once again, Im all turned around here and needing a little help from smart people. Why is it not storing the links in the database?

9
  • And does it give an error message before the one you cite? The one you send isn't helpful to programmers, but the one before is. Commented Nov 2, 2014 at 6:54
  • result is the array of links that goes in there...its before this, just before I declare values for the connection variables. Iknow thats working fine because it outputs the long list of links and says "Inserting into db: link"...I thought I had some malformed sql but do you think it could be that? @Arif_suhail_123 Commented Nov 2, 2014 at 6:59
  • mysqli_error($conn); give this error not the one you giving us at the moment Commented Nov 2, 2014 at 7:01
  • @Jeremy Miller yes it says "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''carads'' at line 1" Commented Nov 2, 2014 at 7:01
  • You got your answer. Commented Nov 2, 2014 at 7:02

3 Answers 3

3

Single quotes (') are used to denote character literals. In your select statement your trying to refer to object (column and table) names, so you should not be using them:

$sql="SELECT adlink FROM carads"; // Note the lack of quotes
$sqlresult = mysqli_query($conn, $sql);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! When I changed the code it gave me a new error. Database not selected. And now ever though it throws the same error when I manually check the database the records are in there. Even though Please Wait 's answer was the "final" correct one, I got to that answer from making the change you suggested. Thanks! @Mureinik
1

I modified some of your code. You did not select any database. Also there exist syntax error in sql string. Try this

$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);

1 Comment

Thanks alot man! You guys are the best here. What did people do before Stack Overflow?
0

Remove single quotes for columns and tables or use grave accent (`) instead. I advise each time you are not sure about SQL queries try to do same operation in phpmyadmin and read the query it used to do that.

1 Comment

Thank you I will be sure to do that in the future @Moein Nourmohammadi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.