PDA

View Full Version : get information from database


error_22
10-19-2005, 03:24 PM
hi, im trying to learn how this works, but sadly i can't get it working. What am I doing wrong? The databse is called "test" and the only table i have is named "logo". There is one field in the logo table which also is named "logo"


$server = "localhost";
$db_name = "dbname"
$user = "user";
$pass = "pass";

$logo="SELECT test * FROM logo";

$db_connection =
mysql_connect("$server","$user","$pass")
or die ("No DB Connection");
$db = mysql_select_db("$db_name",$db_connection)
or die ("Couldn't select DB");

echo "$logo";


the "logo" field contains the URL to my logo. when running this i get a red cross. Can someone help an newbie?

Thanks in advance
Niklas

The

peterska2
10-19-2005, 03:31 PM
Database stuff isn't my strong point, but I believe that this should work.

$server = "localhost";
$db_name = "dbname"
$user = "user";
$pass = "pass";

$logo="SELECT logo * FROM logo";

$db_connection =
mysql_connect("$server","$user","$pass")
or die ("No DB Connection");
$db = mysql_select_db("$db_name",$db_connection)
or die ("Couldn't select DB");

echo "$logo";

DISCLAIMER: As stated at the top of this post, database stuff is NOT my strong point. I strongly reccomend that you back up before attempting this. I will NOT be held responsible for any damage caused by this code. USE THIS AT YOUR OWN RISK

error_22
10-19-2005, 03:43 PM
no it doesnt.... i get a blank page containg this:

SELECT logo * FROM logo

:ermm:

CommuneZoom
10-19-2005, 04:21 PM
With the above code, you are not running the query, rather, you are simply having the script echo out what is between the " ". You need to add more code to have the query run successfully.

I take it from the above coding you are not integrating this with vBulletin? If not, try the below.


$server = "localhost";
$db_name = "dbname"
$user = "user";
$pass = "pass";

$getlogo = mysql_query("SELECT logo FROM logo");
while ($data = mysql_fetch_array($getlogo))
{
echo $data[logo]
}


That does run the query through a loop, though it should provide the desired effect. Alternatively, you may also swicth it to:


$server = "localhost";
$db_name = "dbname"
$user = "user";
$pass = "pass";

$getlogo = mysql_query("SELECT * FROM logo");
while ($data = mysql_fetch_array($getlogo))
{
echo $data[logo]
}


With the above, you can call $data[logo] or $data[anythingelse] from the table.


If you were integrating this with vBulletin, you could just as well use the below and cut back on the coding:


require_once("./global.php");

$getlogo = $db->query_read("SELECT * FROM logo");
while ($data = $db->fetch_array($getlogo))
{
echo $data[logo]
}

error_22
10-19-2005, 06:02 PM
Thank you so much for the help!!

Im doing something wrong though, cause it still doesnt work....

Parse error: parse error, unexpected '}', expecting ',' or ';' in /home/wwwocto2/public_html/kabelkontakten/index.php on line 18

line 18 looks like this:

}
its the one after echo $data[logo]

any thoughts?


Thanks!

Wired1
10-19-2005, 06:46 PM
IIRC this should work:

<?php

$server = "localhost";
$db_name = "dbname"
$user = "user";
$pass = "pass";

$logo="SELECT logo * FROM logo";

// Make a MySQL Connection
mysql_connect($server, $user, $pass) or die(mysql_error());

mysql_select_db($db_name) or die(mysql_error());


// Retrieve all the data from the "example" table
$result = mysql_query($logo) or die(mysql_error());


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "$row <br>";

}


?>

As for the other code posted, the problem was is that it needed a semi-colon ( ; ) after echo $data[logo]

error_22
10-19-2005, 08:52 PM
Thanks so much for your help guys!

Wired1
10-19-2005, 10:25 PM
Ok, this should work (wrote that other one @ work, no code to reference).

<?

// Connect to database
$server = 'localhost';
$user = 'user';
$pass = 'pass';
$db_name = 'dbname';
$connection = mysql_connect($server, $user, $pass)
or die ('I cannot connect to the database because: ' . mysql_error());

$db = mysql_select_db($db_name, $connection)
or die ('I cannot connect to the database because: ' . mysql_error());


$sql = "SELECT * FROM logo";

$result = mysql_query($sql);
if ( mysql_error() )
{ print "Database ERROR: " . mysql_error(); }
else
{
else
while($row = mysql_fetch_array($result))
{
echo "$row[column_1_name] | $row[column_2_name] | $row[column_3_name] | etc... <br>";

}
}//endif else



?>

With this, just replace the column_1_name with the name of the first column, etc.

error_22
10-22-2005, 04:15 PM
thanks a lot :)

error_22
11-18-2005, 09:20 PM
It keeps looping.....i get something similar to:

ResultResultResultResultResultResultResult

I just want "Result" to show up one time :/

Guest190829
11-19-2005, 03:27 AM
Well then your going to have to add a WHERE statement or a Limit Statement..



$sql = "SELECT * FROM logo LIMIT 1";

error_22
11-21-2005, 05:29 PM
thanks a lot =)