...

View Full Version : mysql_query() expects paramater 2 to be resource, string given



Jett61
04-12-2013, 01:46 AM
I'm sure this issue has went through here before, I saw it all over the internet but nothing really made sense to me. I'm pretty novice at using phpMyAdmin, and PHP + MySql.

I have a PHP File for inserting information, that looks like this.

file: a.php


<!DOCYTPE html>
<html>
<head>
<style>
body {
text-align:center;
}
</style>
</head>

<body>
<h1>db.php input</h1>
<form action="db.php" target="_blank" method="post">
Name <input type="text" name="name">
<br>
<br>
Year <input type="text" name="year">
<br>
<br>
Game <input type="text" name="game">
<br>
<br>
<input type="submit" value="send to server">
</form>
</body>
</html>


Then I have my PHP file to INSERT INFO into the database.

file: db.php


<?php
$host = "127.0.0.1";
$user = "root";
$pass = "admin";
$name = $_POST["name"];
$year = $_POST["year"];
$game = $_POST["game"];

$con = mysql_connect($host, $user, $pass)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br><br>";

$select = mysql_select_db("jett61",$con)
or die("Could not select database");
echo "Database selected<br>";

mysql_query($select,"INSERT INTO members (name, year, game) VALUES ($name, $year, $game)")
or die("Insertion into database failed");
echo "Information inserted into database<br>";

mysql_close($con);
?>


And my output looks like the following:



Connected to MySQL

Database selected

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\db.php on line 17
Insertion into database failed


I'm a noob at this, like I said. Any help will be appreciated, thanks.

knightCoder
04-12-2013, 01:54 AM
The second parameter is optional, but if you do specify it, it needs to be a reference to your database connection, not a string as you have currently.

Replace the string with your $con and if your connection and sql query are valid then it should work.

More info on mysql_query is at

http://php.net/manual/en/function.mysql-query.php

Jett61
04-12-2013, 02:00 AM
The second parameter is optional, but if you do specify it, it needs to be a reference to your database connection, not a string as you have currently.

Replace the string with your $con and if your connection and sql query are valid then it should work.

More info on mysql_query is at

http://php.net/manual/en/function.mysql-query.php

Which parts of the code are Parameters 1 and 2 (lol) ? I should have put that into my question as well, my apologies.

felgall
04-12-2013, 04:02 AM
[QUOTE=Jett61;1327693]


<?php
$host = "127.0.0.1";
$user = "root";
$pass = "admin";
$name = $_POST["name"];
$year = $_POST["year"];
$game = $_POST["game"];

$con = mysql_connect($host, $user, $pass)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br><br>";

$select = mysql_select_db("jett61",$con)
or die("Could not select database");
echo "Database selected<br>";

mysql_query($select,"INSERT INTO members (name, year, game) VALUES ($name, $year, $game)")
or die("Insertion into database failed");
echo "Information inserted into database<br>";

mysql_close($con);
?>



It is mysqli_ where the resource goes in the first parameter and the sql in the second the way you have it. With the long dead and soon to be deleted mysql_ calls the sql used to go in the first parameter and the resource optionally in the second.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum