Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

When trying to connect to database get error message

Posted on 2013-12-05
18
Medium Priority
?
1,701 Views
Last Modified: 2013-12-05
$link = mysql_connect('localhost', 'root', 'root');

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25
0
Comment
Question by:burnedfaceless
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698597
What is the code at line 25?
0
 

Author Comment

by:burnedfaceless
ID: 39698612
$link = mysql_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698621
Sorry but the error says mysqli_connect and that line says mysql_connect: are we missing something here?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39698629
Please follow the guidelines in this article.  Choose MySQLi or PDO.  Don't use MySQL any more.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:burnedfaceless
ID: 39698658
Thanks Ray.

I'm still getting the following error and I thought maybe my syntax was wrong.

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

$link = mysqli_connect('localhost', 'root', 'root');
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698692
Except usually one has to declare the database and since probably you're using select_db function, your syntax is right. I presume you'reading the last edition of the Yank's book, since I have the code of an older edition and in chapter 4 there is no index.php. Can you post the index.php whole code?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39698693
0
 

Author Comment

by:burnedfaceless
ID: 39698753
Recently on my mac. I have the fourth edition. I can access the database through the terminal.

<?php
if (get_magic_quotes_gpc())
{
      function stripslashes_deep($value)
      {
            $value = is_array($value) ?
                        array_map('stripslashes_deep', $value) :
                        stripslashes($value);

            return $value;
      }

      $_POST = array_map('stripslashes_deep', $_POST);
      $_GET = array_map('stripslashes_deep', $_GET);
      $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
      $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if (isset($_GET['addjoke']))
{
      include 'form.html.php';
      exit();
}

$link = mysqli_connect('localhost', 'root', 'password');
if (!$link)
{
      $error = 'Unable to connect to the database server.';
      include 'error.html.php';
      exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
      $output = 'Unable to set database connection encoding.';
      include 'output.html.php';
      exit();
}

if (!mysqli_select_db($link, 'ijdb'))
{
      $error = 'Unable to locate the joke database.';
      include 'error.html.php';
      exit();
}

if (isset($_POST['joketext']))
{
      $joketext = mysqli_real_escape_string($link, $_POST['joketext']);
      $sql = 'INSERT INTO joke SET
                  joketext="' . $joketext . '",
                  jokedate=CURDATE()';
      if (!mysqli_query($link, $sql))
      {
            $error = 'Error adding submitted joke: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
      }

      header('Location: .');
      exit();
}

$result = mysqli_query($link, 'SELECT joketext FROM joke');
if (!$result)
{
      $error = 'Error fetching jokes: ' . mysqli_error($link);
      include 'error.html.php';
      exit();
}

while ($row = mysqli_fetch_array($result))
{
      $jokes[] = $row['joketext'];
}

include 'jokes.html.php';
?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698761
Please check the path of included file 'form.html.php'
0
 

Author Comment

by:burnedfaceless
ID: 39698803
It's in the same directory.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698839
So your directory tree is something like:

/Library/
    WebServer/
        Documents/
            yank/
                chapter4/
                    addjoke/
                        index.php
                        form.html.php
                        output.html.php
                        error.html.php

It is right?
I'm sorry for this inquiry, but I don't see errors in the script and I'm trying to guess what's going wrong...
0
 

Author Comment

by:burnedfaceless
ID: 39698896
yes sir

documents is "local host"
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39698939
Mmmmh: in the code of the old edition I read, Yank named his file jokes.php, not jokes.html.php: have you renamed that?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39698955
From http://us3.php.net/manual/en/mysqli.construct.php ...

If you do Not have a default database set up in 'php.ini', the statement is expecting the name of the database to be the fourth parameter.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
0
 

Author Comment

by:burnedfaceless
ID: 39699091
Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 25

Warning: mysqli_set_charset(): Couldn't fetch mysqli in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 34

Warning: include(output.html.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

Warning: include(): Failed opening 'output.html.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/yank/chapter4/addjoke/index.php on line 37

I hate to say it but should I just work through this book from an actual server rather than my computer?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 39699129
Well, that could be a workaround: personally, I leaved localhost since I got several problem when I uploaded my sites/scripts on the server and I hade to adjust paths, external libs and so on: so now I work always on my web server set as production server will be.

But this wouldn't explain whay you're getting all those errors. It really seems something wrong in your localhost paths. Anyway, differently from I thought (comment ID 39698692), you never use select_db function so you should add your database name to your connection line:

$link = mysqli_connect('localhost', 'root', 'password', 'database_name');

I'm sorry I can't give you a better help
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39699180
Thanks for points, but I fear they are undeserved: what is the solution?
0
 

Author Comment

by:burnedfaceless
ID: 39699242
Use the database on the web server.

It will be less work in the long run.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question