You are here: Re: Connecting to mysql with php « PHP Programming Language « IT news, forums, messages
Re: Connecting to mysql with php

Posted by jonathan.beckett on 02/14/06 15:00

Okay... for a start, you have a quote mark missing at the start of your
query.

Here's some boilerplate code that will come in handy...

// you can put these in a config file
$db_server = "locahost";
$db_name = "foo";
$db_username = "root";
$db_password = "";

// description : connects to database and returns handle
function db_connect(){

global $db_server;
global $db_name;
global $db_username;
global $db_password;

$con =@ mysql_connect($db_server,$db_username,$db_password);
if ($con!=false){
if (!(mysql_select_db($db_name,$con))){
// could not connect
}
}
return $con;
}


// how to do a connection...
$con = db_connect();
$sql = "SELECT foo FROM bar";
$result = mysql_query($sql,$con);
if ($result!=false){
if (mysql_num_rows($result)>0){
while ($row =@ mysql_fetch_array($result)){
// $row now holds an associative array of one row from your query
// i.e. do something with it
}
} else {
// no records returned
}
} else {
// problem with SQL
}

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация