Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Location
    around
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    mysql_query() expects paramater 2 to be resource, string given

    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
    Code:
    <!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
    Code:
    <?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:

    Code:
    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.

  • #2
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    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

  • Users who have thanked knightCoder for this post:

    Jett61 (04-13-2013)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Location
    around
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by knightCoder View Post
    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.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    [QUOTE=Jett61;1327693]
    Code:
    <?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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Jett61 (04-13-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •