Page 1 of 1
mysqli -> mysql
Posted: Sat Dec 18, 2010 8:53 am
by barde
i am making a web site, and for connection i am using mysqli, but on the server this extension is not installed, so i guess i need to change the connection to mysql, but i am doing something wrong, please help
this is the code:
Code: Select all
<?php
function db_connect() {
$result = new mysqli('localhost', 'root', 'password', 'pcs');
$result->query("SET NAMES 'utf8'");
if (!$result) {
return false;
}
$result->autocommit(TRUE);
return $result;
}
function db_result_to_array($result) {
$res_array = array();
for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count] = $row;
}
return $res_array;
}
?>
i am trying to change it to something like this:
Code: Select all
function db_connect() {
$result = mysql_connect("localhost","root","");
if (!$result)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("pcs") or die(mysql_error());
mysql_set_charset('utf8');
return $result;
}
thanks in advance!
Re: hello
Posted: Sat Dec 18, 2010 9:18 am
by Da_Elf
ive always used this
Code: Select all
<?php
//login to mysql
function &connectToDb($host, $dbUser, $dbPass, $dbName)
{
// Make connection to MySQL server
if (!$dbConn = @mysql_connect($host, $dbUser, $dbPass)) {
return false;
}
// Select the database
if (!@mysql_select_db($dbName)) {
return false;
}
return $dbConn;
}
$host = 'localhost'; // Hostname of MySQL server
$dbUser = 'name of user'; // Username for MySQL
$dbPass = 'users password'; // Password for user
$dbName = 'database name'; // Database name
$dbConn = &connectToDb($host, $dbUser, $dbPass, $dbName);
?>
then i use tag on $dbconn to the rest of stuff. always worked for me. I keep it in a seperate file outside of my public folder for safe keeping though
Re: hello
Posted: Sat Dec 18, 2010 10:11 am
by barde
it is not working for me,it says
Warning: Missing argument 1 for db_connect(), called in C:\xa
Warning: Missing argument 2 for db_connect(), called in C:\xa
Warning: Missing argument 3 for db_connect(), called in C:\xa
Warning: Missing argument 4 for db_connect(), called in C:\xa
and finally:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in C:\xampp\h

Re: hello
Posted: Sun Dec 19, 2010 2:02 am
by social_experiment
@barde: If you are using the function suggested by Da_Elf you need to add the variables required by the function i.e db_connect($host, $dbUser, $dbPass, $dbName)
Re: hello
Posted: Mon Dec 20, 2010 7:03 am
by barde
ok guys, i think i am going somewhere, i declared the variables, but now this error is coming up:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in C:\xampp\htd...
a am still a noob , so i am really appreciating your help!
thanks
Re: mysqli -> mysql
Posted: Mon Dec 20, 2010 9:32 am
by Weirdan
barde, there are two good habits you should acquire to make your life at these forums better. First is to use syntax highlighting for code you post (there's a handy 'PHP Code' button in post editor). Second is naming your threads descriptively - 'hello' just wouldn't do. Both would make it easier for others to help you - and, as such, get you better answers.
Re: mysqli -> mysql
Posted: Mon Dec 20, 2010 10:32 am
by barde
Weirdan wrote:barde, there are two good habits you should acquire to make your life at these forums better. First is to use syntax highlighting for code you post (there's a handy 'PHP Code' button in post editor). Second is naming your threads descriptively - 'hello' just wouldn't do. Both would make it easier for others to help you - and, as such, get you better answers.
i was in a hurry and i did not paid attention, i will be careful next time. Thanks
Re: mysqli -> mysql
Posted: Mon Dec 20, 2010 12:21 pm
by califdon
Since you are just beginning to learn PHP/MySQL, I think you will learn much faster by reading a few online tutorials, such as
http://www.php-mysql-tutorial.com/wikis ... abase.aspx and dozens of others you can find by entering search terms
php mysql tutorial in the search engine of your choice.
Re: mysqli -> mysql
Posted: Wed Dec 22, 2010 11:55 am
by barde
i finally got it working. After all, the problem was not in the connection, it was in my other file,
function for getting the categories that was not compatible with changed mysqli to mysql code.
thanks to you all.