Solved

When trying to connect to database get error message

Posted on 2013-12-05
18
1,176 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 30

Expert Comment

by:Marco Gasi
Comment Utility
What is the code at line 25?
0
 

Author Comment

by:burnedfaceless
Comment Utility
$link = mysql_connect('localhost', 'root', 'root');
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Sorry but the error says mysqli_connect and that line says mysql_connect: are we missing something here?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 

Author Comment

by:burnedfaceless
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
Please check the path of included file 'form.html.php'
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:burnedfaceless
Comment Utility
It's in the same directory.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
yes sir

documents is "local host"
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
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 30

Expert Comment

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

Author Comment

by:burnedfaceless
Comment Utility
Use the database on the web server.

It will be less work in the long run.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now