mysqli -> mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barde
Forum Newbie
Posts: 5
Joined: Sat Dec 18, 2010 8:47 am

mysqli -> mysql

Post 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!
Last edited by Weirdan on Mon Dec 20, 2010 9:25 am, edited 2 times in total.
Reason: added syntax highlighting
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: hello

Post 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
barde
Forum Newbie
Posts: 5
Joined: Sat Dec 18, 2010 8:47 am

Re: hello

Post 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
:?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: hello

Post 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)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
barde
Forum Newbie
Posts: 5
Joined: Sat Dec 18, 2010 8:47 am

Re: hello

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: mysqli -> mysql

Post 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.
barde
Forum Newbie
Posts: 5
Joined: Sat Dec 18, 2010 8:47 am

Re: mysqli -> mysql

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysqli -> mysql

Post 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.
barde
Forum Newbie
Posts: 5
Joined: Sat Dec 18, 2010 8:47 am

Re: mysqli -> mysql

Post 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.
Post Reply