Jump to content

mysql database connection


Pain

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>";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.