Jump to content

Recommended Posts

Hello guys. Trying to connect php with mysql database and then display results on the screen. This is my code:

 

<?php

$dbhost = "localhost";
$dbuser = "username1";
$dbpass = "password1";
$db = "username1_myDB";

$connection = mysql_connect($dbhost, $dbuser, $dbpass)
or die ("Could not connect");
mysql_select_db($connection, $db);

$show = "SELECT Name, Description FROM people";
$result = mysql_query($show);


while($show = mysql_fetch_array($result)){
$field01 = $show[Name];
$field02 = $show[Description];
echo "id: $field01<br>";
echo "description: $field02<p>";
}


?>

However im getting this:

Warning: mysql_select_db() expects parameter 1 to be string, resource given in /home/pain33/public_html/index.php on line 20

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/pain33/public_html/index.php on line 26

 

Any ideas how to fix this? Thank you.

Link to comment
https://forums.phpfreaks.com/topic/248838-mysql-database-connection/
Share on other sites

Run this:

<?php

$dbhost = "localhost";
$dbuser = "username1";
$dbpass = "password1";
$db = "username1_myDB";

$connection = mysql_connect($dbhost, $dbuser, $dbpass)
or die ("Could not connect");
mysql_select_db($connection, $db);

$show = "SELECT Name, Description FROM people";
$result = mysql_query($show) or trigger_error(mysql_error());


while($show = mysql_fetch_array($result)){
$field01 = $show[Name];
$field02 = $show[Description];
echo "id: $field01<br>";
echo "description: $field02<p>";
}


?>

heres what I use.

//mySQL.php
<?php
    require_once('util.php');

    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_DATABASE', 'illumina_rpg');

$con = "";
function connect_mySQL()
{
	global $con;
	$con = mysql_connect( DB_HOST, DB_USER, DB_PASSWORD);
	if (!$con) die('Could not connect: ' . mysql_error());
	mysql_select_db( DB_DATABASE, $con);
	return $con;
}

function close_mySQL()
{
	global $con;
	mysql_close($con);
}

function write_mySQL( $query )
{
	global $con;
	if (!mysql_query($query,$con)) die('Error: ' . mysql_error());
}

function read_mySQL( $query )
{
	return mysql_query($query);
}

Your mysql_select_db() function had the parameters backwards.  Also, inside your while() loop, you had $array[key] when it needs to be $array['key'].

 

<?php

$dbhost = "localhost";
$dbuser = "username1";
$dbpass = "password1";
$db = "username1_myDB";

$connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to MySQL server.");
mysql_select_db($db, $connection);

$show = "SELECT Name, Description FROM people";
$result = mysql_query($show);


while ($show = mysql_fetch_array($result))
{
    $field01 = $show['Name'];
    $field02 = $show['Description'];
    echo "id: {$field01}<br>";
    echo "description: {$field02}<p>";
}

?>

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.