Jump to content

mysql_query() error in PhP


partTimeRunner

Recommended Posts

I need to send the data gathered from a web form to my database. The connection works, and I can use the data gathered from post to make a query statement that works when used in mysql directly. My php script fails when I call the mysql_query() function. No error is displayed despite having the conditional right after it. My code:

<?phpini_set ("display_errors", "1");error_reporting(E_ALL);     $connection=mysqli_connect(/* not shown */);    if (mysqli_connect_errno())    {      echo "Failed to connect to database: " . mysqli_connect_error();    }     $_clubName = $_POST['clubname'];    $_clubRep = $_POST['clubrep'];    $_meetingID = $_POST['meeting_id'];     $_query = "UPDATE oh_checkin_2015      SET clubrep = '$_clubRep', meeting_2 = $_meetingID    WHERE clubname = '$_clubName'";     $_result = mysql_query($_query);    if (!$_result) die ("Database query failed: " . mysql _error());    else {echo 'successful query';}?>

I have tried my best to locate the problem on my own and have looked up many other forums for a possible solution. If anyone can help me out with this, it would be greatly appreciated.

Link to comment
Share on other sites

Ah! I need to be using VIM to spot these sort of things. Anyway, after fixing the conditional I did end up receiving errors:

'expects parameter 2 to be resource' and 'expects parameter 1 to be mysqli'

which can be fixed by changing the mysql_ funtions into mysqli_

 

Problem solved. Thanks for the help.

Link to comment
Share on other sites

  • 2 weeks later...
See this example---
<?php// This could be supplied by a user, for example$firstname = 'fred';$lastname = 'fox';// Formulate Query// This is the best way to perform an SQL query// For more examples, see mysql_real_escape_string()$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'", mysql_real_escape_string($firstname), mysql_real_escape_string($lastname));// Perform Query$result = mysql_query($query);// Check result// This shows the actual query sent to MySQL, and the error. Useful for debugging.if (!$result) { $message = 'Invalid query: ' . mysql_error() . "n"; $message .= 'Whole query: ' . $query; die($message);}// Use result// Attempting to print $result won't allow access to information in the resource// One of the mysql result functions must be used// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.while ($row = mysql_fetch_assoc($result)) { echo $row['firstname']; echo $row['lastname']; echo $row['address']; echo $row['age'];}// Free the resources associated with the result set// This is done automatically at the end of the scriptmysql_free_result($result);?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...