Jump to content

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


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*.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.