Jump to content

[SOLVED] Sorry! Wrong forum, please move to MySQL


hinges

Recommended Posts

Edit: Sorry, I've posted this in the wrong forum. If a mod could move it to the MySQL forum, that'd be great.

 

Using either of these always results in the "ERROR: The statement failed to prepare." message.

<?php
   $mysqli=new mysqli('**********', '**********', '**********');
   if (mysqli_connect_errno())
   {
    echo '<p>ERROR: Could not connect to database.</p>';
    exit();
   }
   if ($stmt = $mysqli->prepare('SELECT * FROM sometable WHERE somefield=?'))
   {
    echo '<p>The statement prepared successfuly.</p>';
   }
   else
   {
    echo '<p>ERROR: The statement failed to prepare.</p>';
   }
   $mysqli->close();
  ?>

 

<?php
   $link=mysqli_connect('**********', '**********', '**********');
   if (mysqli_connect_errno())
   {
    echo '<p>ERROR: Could not connect to database.</p>';
    exit();
   }
   if ($stmt = mysqli_prepare($link, 'SELECT * FROM sometable WHERE somefield=?'))
   {
    echo '<p>The statement prepared successfuly.</p>';
   }
   else
   {
    echo '<p>ERROR: The statement failed to prepare.</p>';
   }
   mysqli_close($link);
  ?>

 

I'm new to this, but I don't think theres anything wrong with the code here, does anyone have any suggestions as to what might be causing this?

 

Thanks in advance,

hinges.

I'm pretty sure it's connecting ok, I've just tried this:

<?php
   if($mysqli=new mysqli('**********', '********', '********'))
   {
    if (mysqli_connect_errno())
    {
     echo '<p>DB ERROR: MySQLi generated an error.</p>';
     exit();
    }
    if ($result = $mysqli->query("SELECT * FROM sometable WHERE somefield='value'"))
    {
     echo '<p>Success!</p>';
 $result->close();
    }
    else
    {
     echo '<p>QUERY ERROR: MySQLi->Query returned false.</p>';
    }
    $mysqli->close();
   }
   else
   {
    echo '<p>DB ERROR: new MySQLi returned false.</p>';
   }
  ?>

and I get the Query Error message every time.

Are there any known situations in which you can successfully connect to a database but not query it?

"No database selected"

 

Damn I feel stupid. I've put the database name as the fourth argument of "new mysqli", and it works fine.

 

Thanks!

 

Edit: I've worked out why I expected it to work without specifying a database in the mysqli constructor - last time I did this, i always specified the table as 'dbname'.'sometable', but this time I copied my SQL from php.net *facepalm*.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.