hinges Posted December 2, 2008 Share Posted December 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/ Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 Do you have any tables in your database? Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704353 Share on other sites More sharing options...
hinges Posted December 2, 2008 Author Share Posted December 2, 2008 Do you have any tables in your database? Yes. My database contains a table named "sometable" which has two fields, including "somefield". Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704357 Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 if ($stmt = $mysqli->prepare("SELECT * FROM sometable WHERE somefield='?'")) is it mean to equal a question mark? and if yes try it with those quotation marks Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704360 Share on other sites More sharing options...
hinges Posted December 2, 2008 Author Share Posted December 2, 2008 The question mark is a parameter marker. I will use mysqli_stmt::bind_param to insert the value I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704362 Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 In which case, your php is perfect. I think you're right, maybe a different forum Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704366 Share on other sites More sharing options...
gevans Posted December 2, 2008 Share Posted December 2, 2008 though php.net shows 4 values in mysqli_connect() you only have 3? Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704367 Share on other sites More sharing options...
hinges Posted December 3, 2008 Author Share Posted December 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704704 Share on other sites More sharing options...
dclamp Posted December 3, 2008 Share Posted December 3, 2008 put this at the end: echo $mysqli->error(); Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704727 Share on other sites More sharing options...
Zane Posted December 3, 2008 Share Posted December 3, 2008 you didn't need to change the title of your topic to have it moved. editing your post was sufficient. Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704743 Share on other sites More sharing options...
hinges Posted December 3, 2008 Author Share Posted December 3, 2008 put this at the end: echo $mysqli->error(); Interestingly, this causes an error of it's own: Fatal error: Call to undefined method mysqli::error() Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-704759 Share on other sites More sharing options...
dclamp Posted December 4, 2008 Share Posted December 4, 2008 put this at the end: echo $mysqli->error(); Interestingly, this causes an error of it's own: Fatal error: Call to undefined method mysqli::error() oops, my fault. It isnt a function. try this: $mysqli->error Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-705581 Share on other sites More sharing options...
hinges Posted December 5, 2008 Author Share Posted December 5, 2008 "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*. Quote Link to comment https://forums.phpfreaks.com/topic/135227-solved-sorry-wrong-forum-please-move-to-mysql/#findComment-706584 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.